package org.millioncall.yueyoga.admin.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Coach;
import org.millioncall.yueyoga.admin.model.Group;
import org.millioncall.yueyoga.admin.model.User;
import org.millioncall.yueyoga.admin.service.CoachService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

//--created by lisj on 2014.03.06
@Controller
public class CoachAction extends BaseAction {
	private CoachService coachService;

	public CoachService getCoachService() {
		return coachService;
	}

	public void setCoachService(CoachService coachService) {
		this.coachService = coachService;
	}

	// 查询老师信息
	@RequestMapping(value = "/findCoachList.jhtml")
	public String getCoachByPage(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		super.initFrontHeader(request, model);

		int start = 0;
		int limit = 10;
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");

		if (StringUtils.isNotBlank(pageSize)) {
			limit = Integer.parseInt(pageSize);
		}
		
		if (StringUtils.isNotBlank(pageNo)) {
			start = (Integer.parseInt(pageNo) - 1) * limit;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(request.getParameter("name"))) {
			map.put("name", "%" + request.getParameter("name") + "%");
			model.put("names", request.getParameter("name"));
		}

		if (StringUtils.isNotBlank(request.getParameter("type"))) {
			map.put("type", request.getParameter("type"));
			model.put("type", request.getParameter("type"));
		}

		if (StringUtils.isNotBlank(request.getParameter("sex"))) {
			map.put("sex", request.getParameter("sex"));
			model.put("sex", request.getParameter("sex"));
		}
		
		List<Integer> groupIds = new ArrayList<Integer>();
		for (Group group : this.getUser(request).getGroups()) {
			groupIds.add(group.getId());
		}
		map.put("groups", groupIds);
		map.put("hallcode", this.getUser(request).getOrganizationId());
		PaginatedList<Coach> p = null;
		p = coachService.getCoachInPage(start, limit, map);

		model.put("totalspeakers", p.getPager().getCountOfTotalResults());
		model.put("coachList", p);
		return "coach_search";
	}

	// 跳转到添加老师页面
	@RequestMapping(value = "/toAddCoach.jhtml")
	public String toAddCoach(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		super.initFrontHeader(request, map);
		return "coach_add";
	}

	// 添加老师信息
	@RequestMapping(value = "/addCoach.jhtml")
	public String addCoach(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		super.initFrontHeader(request, model);

		String name = request.getParameter("name");
		if (StringUtils.isBlank(name)) {
			model.put("msg", "老师姓名不可为空");
			return "coach_add";
		}
		if (name.length() > 10) {
			model.put("msg", "老师姓名长度应小于11个字");
			return "coach_add";
		}
		User user = this.getUser(request);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("organizationId", user.getOrganizationId());
		map.put("name", name);
		map.put("sex", request.getParameter("sex"));
		map.put("type", request.getParameter("type"));
		map.put("remarks", request.getParameter("remarks"));
		map.put("enable", Boolean.parseBoolean(request.getParameter("enable")));
		Date now = new Date();
		map.put("createTime", now);
		map.put("lastModifyTime", now);
		map.put("createdBy", super.getUser(request).getId());
		map.put("lastModifiedBy", super.getUser(request).getId());

		if (coachService.saveCoach(map)) {
			return "redirect:findCoachList.jhtml";
		} else {
			model.put("msg", "添加教练信息失败，请稍候重试或联系管理员");
			return "coach_add";
		}
	}

	// 到编辑老师页面
	@RequestMapping(value = "/toEditCoach.jhtml")
	public String toEditCoach(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		super.initFrontHeader(request, model);
		Integer id = Integer.parseInt(request.getParameter("id"));
		Coach coach = coachService.findById(id);
		model.put("coach", coach);
		return "coach_edit";
	}

	// 编辑老师信息
	@RequestMapping(value = "/editCoach.jhtml")
	public String editCoach(ModelMap context, HttpServletRequest request,
			HttpServletResponse response) {

		super.initFrontHeader(request, context);

		Integer id = Integer.parseInt(request.getParameter("coachId"));
		String name = request.getParameter("name");
		if (StringUtils.isBlank(name)) {
			context.put("msg", "教练信息性名不可为空");
			return "coach_edit";
		}
		if (name.length() > 15) {
			context.put("msg", "教练信息姓名长度应介于2-15之间");
			return "coach_edit";
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("coachId", id);
		map.put("name", name);
		map.put("type", request.getParameter("type"));
		map.put("sex", request.getParameter("sex"));
		map.put("remarks", request.getParameter("remarks"));
		map.put("enable", Boolean.parseBoolean(request.getParameter("enable")));
		Date now = new Date();
		map.put("lastModifyTime", now);
		map.put("lastModifiedBy", super.getUser(request).getId());
		//map.put("hallId", user.getOrganizationId());

		if (coachService.updateCoach(map)) {
			return "redirect:findCoachList.jhtml";
		} else {
			context.put("msg", "更新教练信息失败，请稍候重试或联系管理员");
			return "coach_edit";
		}
	}

	// 删除教练信息
	@RequestMapping(value = "/deleteCoach.jhtml")
	public void deleteCoach(HttpServletRequest request,
			HttpServletResponse response) {
		String msg = null;
		String ids = request.getParameter("ids");
		if (StringUtils.isBlank(ids)) {
			msg = "{'success':false,'msg':'未提交待删除教练信息的编号'}";
		} else {
			String[] arrids = ids.split(",");
			Integer[] deids = new Integer[20];
			for (int i = 0; i < arrids.length; i++) {
				if (arrids[i] != null && !arrids[i].equals("")) {
					deids[i] = Integer.parseInt(arrids[i]);
				}
			}
			try {
				coachService.deleteCoachByIds(deids);
				msg = "true";
			} catch (RuntimeException re) {
				msg = "false";
			}
		}
		WebAppUtils.ajaxResponse(response, msg);
	}
}
